#include <iostream>
#include "Scope.h"
#include "Reader.h"
#include "Tokenizer.h"
#include "Scuba.h"
#include "Indenter.h"

#include <string>
#include <conio.h>

#include "ArgumentParser.h"
#include "Helper.h"

std::string gsFileName = "./test.c";

CArgumentParser* gpxArgumentParser;
IndenterOptions gxIndenterOptions;

using namespace std;

Reader xReader;
Tokenizer xTokenizer;
Scuba xScubaDiver;
Indenter xIndenter;

int parseCommandLineIndenter(int argc, char**argv)
{
	gpxArgumentParser=new CArgumentParser("h");

	gpxArgumentParser->m_Helper->fillProgramName("nTorpedo");
	gpxArgumentParser->m_Helper->fillAuthor("SonOfDelphi");
	gpxArgumentParser->m_Helper->fillSummary("Source Code Indenter");
	gpxArgumentParser->m_Helper->fillUsageLine("[OPTIONS] <SourceFile>");
	gpxArgumentParser->fillHelpForOption('h',"Displays this help message");

	gpxArgumentParser->m_Helper->fillYear("2009");
	gpxArgumentParser->m_Helper->fillOwner("The Oraclique");
	gpxArgumentParser->m_Helper->fillTailMessage("Part of Neptuner Code-Management Suite.\nBuild:" __DATE__"/"__TIME__  "\nThis program comes with absolutely no warranty.");

	int nParseEndPos = gpxArgumentParser->parse(argc,argv);
	
	if(!gpxArgumentParser->wasParseSuccessful())
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		cerr<<"Invalid option supplied"<<endl;
		return -1;
	}

	return nParseEndPos+1;
}

int main(int argc, char**argv)
{
	int nProgArgIndex = parseCommandLineIndenter(argc, argv);
	if(nProgArgIndex<0)
	{
		return 1;
	}

	if(gpxArgumentParser->wasOptionSupplied('h'))
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		return 0;
	}

	if(nProgArgIndex<argc)
	{
			gsFileName=argv[nProgArgIndex];
	}
	else
	{
		cerr<<"Attempting to proceed with default test file: "<<gsFileName<<endl;
	}

    xReader.gobble(gsFileName);
    xTokenizer.tokenize(xReader.getStream());
    xScubaDiver.setObserver(&xIndenter);
    int nListSize = (int)xTokenizer.m_asTokens.size();
    for(int nLooper = 0; nLooper<nListSize; nLooper++)
    {
        xScubaDiver.feed(xTokenizer.m_asTokens[nLooper]);
    }

	if(argc==1)
	{
		cerr<<"Press any key to continue..";
		_getch();
	}
    return 0;
}





